package com.adapters.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;

import com.objects.GroupMember;
import com.objects.SecurePackageLog;

public class SecurePackageLogTableAdapter {
	public static final String TABLE_NAME = ContentProviderDB.PREFIX
			+ "secure_package_log";
	public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
			+ TABLE_NAME + " (" + ContentProviderDB.COL_DATE + " text, "
			+ ContentProviderDB.COL_DESCRIPTION + " text, "
			+ ContentProviderDB.COL_STATUS + " text, "
			+ ContentProviderDB.COL_CONTACT_ID + " integer not null, "
			+ ContentProviderDB.COL_ORDER + " integer not null)";

	private Context mContext;

	public SecurePackageLogTableAdapter(Context context) {
		mContext = context;
	}

	public void add(SecurePackageLog item, long contactID, int order) {
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		ContentValues values = new ContentValues();
		values.put(ContentProviderDB.COL_DATE, item.getDate());
		values.put(ContentProviderDB.COL_CONTACT_ID, contactID);
		values.put(ContentProviderDB.COL_DESCRIPTION, item.getDescription());
		values.put(ContentProviderDB.COL_STATUS, item.getStatus());
		values.put(ContentProviderDB.COL_ORDER, order);
		mContext.getContentResolver().insert(contentUri, values);

	}

	public boolean clear() {
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		int value = mContext.getContentResolver()
				.delete(contentUri, null, null);
		return value > 0;

	}

	public boolean clear(long contactID) {
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		int value = mContext.getContentResolver().delete(contentUri,
				ContentProviderDB.COL_CONTACT_ID + "=" + contactID, null);
		return value > 0;

	}

	public ArrayList<SecurePackageLog> getItems(long contactID)
			throws SQLException {
		ArrayList<SecurePackageLog> results = new ArrayList<SecurePackageLog>();
		Uri contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI,
				TABLE_NAME);
		Cursor mCursor = mContext.getContentResolver().query(contentUri, null,
				ContentProviderDB.COL_CONTACT_ID + "=" + contactID, null,
				ContentProviderDB.COL_ORDER + " ASC");
		if (mCursor.moveToFirst()) {
			do {
				SecurePackageLog object = new SecurePackageLog();
				object.setDate(mCursor.getString(mCursor
						.getColumnIndex(ContentProviderDB.COL_DATE)));
				object.setDescription(mCursor.getString(mCursor
						.getColumnIndex(ContentProviderDB.COL_DESCRIPTION)));
				object.setStatus(mCursor.getString(mCursor
						.getColumnIndex(ContentProviderDB.COL_STATUS)));
				results.add(object);
			} while (mCursor.moveToNext());
		}
		mCursor.close();
		return results;
	}

}
